// src/composables/usePagination.js
import { ref, computed } from 'vue';

export default function usePagination(filteredData) {
  const currentPage = ref(1);
  const itemsPerPage = ref(3);

  const totalPages = computed(() => {
    return Math.ceil(filteredData.value.length / itemsPerPage.value);
  });

  const currentData = computed(() => {
    const startIndex = (currentPage.value - 1) * itemsPerPage.value;
    const endIndex = startIndex + itemsPerPage.value;
    return filteredData.value.slice(startIndex, endIndex);
  });

  function firstPage() {
    currentPage.value = 1;
  }

  function prevPage() {
    if (currentPage.value > 1) {
      currentPage.value--;
    }
  }

  function nextPage() {
    if (currentPage.value < totalPages.value) {
      currentPage.value++;
    }
  }

  function lastPage() {
    currentPage.value = totalPages.value;
  }

  return {
    currentPage,
    itemsPerPage,
    totalPages,
    currentData,
    firstPage,
    prevPage,
    nextPage,
    lastPage
  };
}
